package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneBumenDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrDeptDao;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneBumenEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeptService {
    @Autowired
    UsrDeptDao usrDeptDao;
    @Autowired
    EneBumenDao eneBumenDao;

    @Value("${companyId}")
    String companyId;

    private Logger logger = LoggerFactory.getLogger(DeptService.class);

    public void syncDept() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        List<EneBumenEntity> eneWuziChangshangEntities = eneBumenDao.selectByExample(null);
        for (EneBumenEntity eneBumenEntity :
                eneWuziChangshangEntities) {
            try {
                String bmId = eneBumenEntity.getBmId();
                String bmMc = HzhrTypeUtil.object2string(eneBumenEntity.getBmMc());

                String orgId = eneBumenEntity.getOrgId();
                String bmCode = eneBumenEntity.getBmCode();
                if (orgId.length() <= 4 || bmCode.length() <= 4) {
                    continue;
                }
                orgId = orgId.replaceFirst("0000", "01000901");
                bmCode = bmCode.replace("0000", companyId + "-01");

                //新增或更新部门(所有上级部门置位0)
                UsrDeptExample usrDeptExample = new UsrDeptExample();
                usrDeptExample.createCriteria().andOutIdEqualTo(bmId);
                List<UsrDeptEntity> usrDeptEntities = usrDeptDao.selectByExample(usrDeptExample);
                if (usrDeptEntities.size() > 0) {
                    UsrDeptEntity usrDeptEntity = usrDeptEntities.get(0);
                    usrDeptEntity.setOutId(bmId);
                    usrDeptEntity.setName(bmMc);
                    usrDeptEntity.setCode(bmCode);
                    usrDeptEntity.setLevel((bmCode.length() - 4) / 2 + 1);
                    usrDeptEntity.setParentId(0);
                    usrDeptEntity.setCompanyId(Integer.parseInt(companyId));
                    usrDeptEntity.setUpdateTime(new Date());
                    usrDeptEntity.setUpdateUserId(0);
                    usrDeptEntity.setResourceCode(orgId);
                    usrDeptEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrDeptEntity.setState("0");
                    usrDeptDao.updateByPrimaryKey(usrDeptEntity);
                    updateCount++;
                } else {
                    UsrDeptEntity usrDeptEntity = new UsrDeptEntity();
                    usrDeptEntity.setOutId(bmId);
                    usrDeptEntity.setName(bmMc);
                    usrDeptEntity.setCode(bmCode);
                    usrDeptEntity.setLevel((bmCode.length() - 4) / 2 + 1);
                    usrDeptEntity.setParentId(0);
                    usrDeptEntity.setCompanyId(Integer.parseInt(companyId));
                    usrDeptEntity.setCreateTime(new Date());
                    usrDeptEntity.setCreateUserId(0);
                    usrDeptEntity.setResourceCode(orgId);
                    usrDeptEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrDeptEntity.setState("0");
                    usrDeptDao.insert(usrDeptEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //更新部门的上级部门
        List<UsrDeptEntity> usrDeptEntities = usrDeptDao.selectByExample(null);
        for (UsrDeptEntity usrDeptEntity :
                usrDeptEntities) {
            try {
                String code = usrDeptEntity.getCode();
                String parentCode = code.substring(0, code.length() - 2);

                for (UsrDeptEntity usrDeptEntity1 :
                        usrDeptEntities) {
                    String code1 = usrDeptEntity1.getCode();
                    if (code1.equals(parentCode)) {
                        usrDeptEntity.setParentId(usrDeptEntity1.getId());
                        usrDeptEntity.setParentName(usrDeptEntity1.getName());
                        usrDeptDao.updateByPrimaryKey(usrDeptEntity);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        long end = new Date().getTime();
        logger.info("同步部门结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");

    }
}
